package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsHazardIndentificationResultTypeEnum;
import qc.common.core.enums.EhsRiskLevelEnum;
import qc.module.ehs.dto.hazard.EhsHazardInfoDto;
import qc.module.ehs.entity.EhsHazardInfo;

import java.util.List;

/**
 * EhsHazardInfoMapper
 *
 * @author QuCheng Tech
 * @create 2024/6/13
 */
@Mapper
public interface EhsHazardInfoMapper {
    EhsHazardInfoMapper MAPPER = Mappers.getMapper(EhsHazardInfoMapper.class);

    @Mapping(target = "recentidentificationtm", source = "recentidentificationtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "evltm", source = "evltm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "identificationresult", ignore = true)
    @Mapping(target = "risklevel", ignore = true)
    EhsHazardInfo toEntity(EhsHazardInfoDto dto);

    default EhsHazardInfo dtoToEntity(EhsHazardInfoDto dto) {
        EhsHazardInfo en = EhsHazardInfoMapper.MAPPER.toEntity(dto);

        en.setIdentificationresult(getEhsHazardIndentificationResultTypeEnum(dto.getIdentificationresult()));
        en.setRisklevel(getEhsRiskLevelEnum(dto.getRisklevel()));

        return en;
    }

    @Mapping(target = "recentidentificationtm", source = "recentidentificationtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "evltm", source = "evltm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "identificationresult", source = "identificationresult.index")
    @Mapping(target = "risklevel", source = "risklevel.index")
    EhsHazardInfoDto toDto(EhsHazardInfo entity);
    
    List<EhsHazardInfoDto> toDtoList(List<EhsHazardInfo> ens);

    static EhsHazardIndentificationResultTypeEnum getEhsHazardIndentificationResultTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsHazardIndentificationResultTypeEnum item : EhsHazardIndentificationResultTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return EhsHazardIndentificationResultTypeEnum.NOT_EXIST;
    }

    static EhsRiskLevelEnum getEhsRiskLevelEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsRiskLevelEnum item : EhsRiskLevelEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        
        //风险等级必须有，默认为null
        return null;
    }
}
